package com.net.framework.help.application;

import android.os.Handler;
import android.os.Looper;

import com.net.framework.help.manager.ActivityStackManager;
import com.net.framework.help.utils.LogUtils;
import com.net.framework.help.utils.UIUtils;
import com.tencent.smtt.sdk.QbSdk;

/**
 * @author zhangxiaobin
 * @version 1.1.0
 * @email zhangxiaobin@yipintech.com
 * @date 创建时间：2015年7月10日 上午9:01:57
 * @explain Application基类
 */
public class BaseApplication extends com.activeandroid.app.Application {
    private long exitTime = 0;
    private static long DELAYTIME = 2000;// 双击返回键退出程序区间时间
    private static BaseApplication mContext = null;

    private static Handler mMainThreadHandler = null;

    private static Looper mMainThreadLooper = null;

    private static Thread mMainThead = null;

    private static int mMainTheadId;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        this.mContext = this;
        this.mMainThreadHandler = new Handler();
        this.mMainThreadLooper = getMainLooper();
        this.mMainThead = Thread.currentThread();
        this.mMainTheadId = android.os.Process.myTid();// 主線程id
    }

    /**
     * 初始化腾讯X5内核
     */
    public void initX5() {
        //搜集本地tbs内核信息并上报服务器，服务器返回结果决定使用哪个内核。
        QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
            @Override
            public void onViewInitFinished(boolean arg0) {
                // TODO Auto-generated method stub
                //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
                LogUtils.println("加载内核onViewInitFinished is==" + arg0);
            }

            @Override
            public void onCoreInitFinished() {
                // TODO Auto-generated method stub
            }
        };
        //x5内核初始化接口
        QbSdk.initX5Environment(getApplicationContext(), cb);
    }

    /***
     * 获取到主线程的上下文
     *
     * @return
     */
    public static BaseApplication getApplication() {
        return mContext;
    }

    /***
     * 获取到主线程的handler
     *
     * @return
     */
    public static Handler getMainThreadHandler() {
        return mMainThreadHandler;
    }

    /***
     * 获取到主线程的looper
     *
     * @return
     */
    public static Looper getMainThreadLooper() {
        return mMainThreadLooper;
    }

    /***
     * 获取到主线程
     *
     * @return
     */
    public static Thread getMainThread() {
        return mMainThead;
    }

    /***
     * 获取到主线程的id
     *
     * @return
     */
    public static int getMainThreadId() {
        return mMainTheadId;
    }

    /**
     * 点击两次返回退出应用
     */
    public void dblclickExit() {
        if ((System.currentTimeMillis() - exitTime) > DELAYTIME) {
            UIUtils.shortToast("再按一次退出程序");
            exitTime = System.currentTimeMillis();
        } else {
            systemExit();
        }
    }

    /**
     * 退出应用程序时清除
     */
    public void systemExit() {
        try {
            ActivityStackManager.getInstance().finishAll();
            System.exit(0);
        } catch (Exception e) {
        }
    }
}
